package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.AgoniRewardMapper;
import com.ruoyi.system.mapper.AgoniTodoEventMapper;
import com.ruoyi.system.pojo.dto.CreateTodoEventDTO;
import com.ruoyi.system.pojo.entity.Reward;
import com.ruoyi.system.pojo.entity.TodoEvent;
import com.ruoyi.system.pojo.vo.TodoEventRewardVO;
import com.ruoyi.system.service.IAgoniTodoEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IAgoniTodoEventServiceImlp implements IAgoniTodoEventService {
    @Autowired
    private AgoniTodoEventMapper eventMapper;

    @Autowired
    private AgoniRewardMapper rewardMapper;



    @Override
    public List<TodoEvent> getAllTodoEventsCommon() {



        List<TodoEvent> events = eventMapper.selectAllEvents();





        return events;
    }


    //App端获取所有待办事件
    @Override
    public List<TodoEventRewardVO> getAllTodoEvents() {

        //创建返回值对象
        List<TodoEventRewardVO> eventRewardVos=new ArrayList<>();

        List<TodoEvent> events = eventMapper.selectAllEvents();

        //对每个事件进行处理
        for(TodoEvent event:events){

            //根据event里面的rewardId获取奖励信息
            if(event.getRewardId()==null){
                continue;
            }
            Reward reward=rewardMapper.selectRewardById(Long.valueOf(event.getRewardId()));

            //设置信息
            //创建奖励对象
            TodoEventRewardVO eventRewardVo= TodoEventRewardVO.builder()
                    .id(event.getId())
                    .dueDate(event.getDueDate())
                    .isCompleted(event.getIsCompleted())
                    .rewardId(event.getRewardId())
                    .activityId(event.getActivityId())
                    .createdAt(event.getCreatedAt())
                    .task(event.getTask())
                    .startDate(event.getStartDate())
                    .item(reward.getItem())
                    .eventId(event.getEventId())
                    .title(event.getTitle())
                    .updatedAt(event.getUpdatedAt())
                    .type(reward.getType())
                    .description(reward.getDescription())
                    .grade(reward.getGrade())
                    .competitionId(event.getCompetitionId())
                    .build();
            //添加到返回值对象中
            eventRewardVos.add(eventRewardVo);


    };
        return eventRewardVos;
    }


    //App端获取用户的待办事件


    @Override
    public List<TodoEventRewardVO> getTodoEventsByUserId(Long userId) {
        //创建返回值对象
        List<TodoEventRewardVO> eventRewardVos1=new ArrayList<>();
        List<TodoEvent> todoEvents=eventMapper.selectTodoEventsByUserId(userId);
        //对每个事件进行处理
        for(TodoEvent event:todoEvents){

            //根据event里面的rewardId获取奖励信息
            if(event.getRewardId()==null){
                continue;
            }
            Reward reward=rewardMapper.selectRewardById(Long.valueOf(event.getRewardId()));

            //设置信息
            //创建奖励对象
            TodoEventRewardVO eventRewardVo= TodoEventRewardVO.builder()
                    .id(event.getId())
                    .dueDate(event.getDueDate())
                    .isCompleted(event.getIsCompleted())
                    .rewardId(event.getRewardId())
                    .activityId(event.getActivityId())
                    .createdAt(event.getCreatedAt())
                    .task(event.getTask())
                    .startDate(event.getStartDate())
                    .item(reward.getItem())
                    .eventId(event.getEventId())
                    .title(event.getTitle())
                    .updatedAt(event.getUpdatedAt())
                    .type(reward.getType())
                    .description(reward.getDescription())
                    .grade(reward.getGrade())
                    .competitionId(event.getCompetitionId())
                    .status(event.getStatus())
                    .userId(event.getUserId())
                    .build();
            //添加到返回值对象中
            eventRewardVos1.add(eventRewardVo);

        };
        return   eventRewardVos1;
    }



    //APP端用户创建自己的待办事件


    @Override
    public int createTodoEvent(CreateTodoEventDTO createTodoEventDTO, Long userId) {
        //创建待办事件对象
        TodoEvent todoEvent=TodoEvent.builder()
               .dueDate(createTodoEventDTO.getDueDate())
               .isCompleted(createTodoEventDTO.getIsCompleted())
               .rewardId(createTodoEventDTO.getRewardId())
               //.activityId(createTodoEventDTO.getActivityId())
               .task(createTodoEventDTO.getTask())
               .startDate(createTodoEventDTO.getStartDate())
               .title(createTodoEventDTO.getTitle())
              // .competitionId(createTodoEventDTO.getCompetitionId())
               .status(createTodoEventDTO.getStatus())
                .priority(createTodoEventDTO.getPriority())
                .userId(Math.toIntExact(userId))
               .build();
        //插入数据库
        int result=eventMapper.insertTodoEvent(todoEvent);
        return result;
    }


    //app端删除事件


    @Override
    public int deleteTodoEvent(Long id) {
        return eventMapper.deleteTodoEvent(id);
    }
}
